package cn.iocoder.yudao.module.scrm.dal.mysql.marketing;


import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.scrm.controller.admin.marketing.vo.content.MarketingContentPageReqByTypeVO;
import cn.iocoder.yudao.module.scrm.controller.admin.marketing.vo.content.MarketingContentPageReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.marketing.vo.content.MarketingContentRespByTypeVO;

import cn.iocoder.yudao.module.scrm.dal.dataobject.marketing.MarketingCategoryDO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.marketing.MarketingContentDO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.marketing.MarketingTransmitDO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.marketing.MarketingVisitorDO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.order.OrderDO;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 营销内容 Mapper
 *
 * @author admin
 */
@Mapper
public interface MarketingContentMapper extends BaseMapperX<MarketingContentDO> {

    default PageResult<MarketingContentDO> selectPage(MarketingContentPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<MarketingContentDO>()
                .inIfPresent(MarketingContentDO::getCategoryId, reqVO.getCategoryId())
                .eqIfPresent(MarketingContentDO::getMarketingType, reqVO.getMarketingType())
                .inIfPresent(MarketingContentDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(MarketingContentDO::getDeptId, reqVO.getDeptId())
                .likeIfPresent(MarketingContentDO::getDeptName, reqVO.getDeptName())
                .eqIfPresent(MarketingContentDO::getTitle, reqVO.getTitle())
                .likeIfPresent(MarketingContentDO::getName, reqVO.getName())
                .eqIfPresent(MarketingContentDO::getContent, reqVO.getContent())
                .eqIfPresent(MarketingContentDO::getSource, reqVO.getSource())
                .eqIfPresent(MarketingContentDO::getIsIssue, reqVO.getIsIssue())
                .eqIfPresent(MarketingContentDO::getCoverUrl, reqVO.getCoverUrl())
                .eqIfPresent(MarketingContentDO::getPicturesUrl, reqVO.getPicturesUrl())
                .eqIfPresent(MarketingContentDO::getVideoUrl, reqVO.getVideoUrl())
                .eqIfPresent(MarketingContentDO::getDisplayUrl, reqVO.getDisplayUrl())
                .eqIfPresent(MarketingContentDO::getMaterialType, reqVO.getMaterialType())
                .eqIfPresent(MarketingContentDO::getRemarkV, reqVO.getRemarkV())
                .betweenIfPresent(MarketingContentDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MarketingContentDO::getId));
    }


    default PageResult<MarketingContentRespByTypeVO> selectByTypePage(MarketingContentPageReqByTypeVO reqVO) {

        MPJLambdaWrapper<MarketingContentDO> query = new MPJLambdaWrapper<MarketingContentDO>()
                .selectAll(MarketingContentDO.class)//主表字段
                .selectAs(MarketingCategoryDO::getName, MarketingContentRespByTypeVO::getCategoryName)//返回分类表的名称
                .selectAs(MarketingCategoryDO::getDeptId, MarketingContentRespByTypeVO::getDeptId)//返回分类表部门id集合

                .selectSum(MarketingTransmitDO::getShareCount,MarketingContentRespByTypeVO::getShareCount)//返回转发表的总和-分享次数
                .selectSum(MarketingTransmitDO::getBrowseCount,MarketingContentRespByTypeVO::getBrowseCount)//返回转发表的总和-浏览人数
                //.selectSum(MarketingVisitorDO::getShareCount,MarketingContentRespByTypeVO::getTransmitCount)//返回营销访客数据表的总和-访客转发次数
                .selectSum(MarketingTransmitDO::getGainCustomerCount,MarketingContentRespByTypeVO::getGainCustomerCount)

                .eq(StringUtils.hasText(reqVO.getFolderType()), MarketingContentDO::getFolderType, reqVO.getFolderType())
                .eq(StringUtils.hasText(reqVO.getParentId()), MarketingContentDO::getParentId, reqVO.getParentId())
                .eq(StringUtils.hasText(reqVO.getFileType()), MarketingContentDO::getFileType, reqVO.getFileType())
                .eq(StringUtils.hasText(reqVO.getFileSize()), MarketingContentDO::getFileSize, reqVO.getFileSize())
                .eq((reqVO.getIsIssue()!=null), MarketingContentDO::getIsIssue, reqVO.getIsIssue())

                .eq(StringUtils.hasText(reqVO.getMarketingType()), MarketingContentDO::getMarketingType, reqVO.getMarketingType())
                .like(StringUtils.hasText(reqVO.getTitle()), MarketingContentDO::getTitle, reqVO.getTitle())
                .like(StringUtils.hasText(reqVO.getCreatorName()), MarketingContentDO::getCreatorName, reqVO.getCreatorName())

                //关键词查询-小程序
                .or().like(StringUtils.hasText(reqVO.getKey()), MarketingContentDO::getTitle, reqVO.getKey())
                .or().like(StringUtils.hasText(reqVO.getKey()),MarketingContentDO::getCreatorName,reqVO.getKey())
                .between(reqVO.getStartTime()!=null && reqVO.getEndTime()!=null,
                        MarketingContentDO::getCreateTime,reqVO.getStartTime(),reqVO.getEndTime())

                // 集合入参id查询-多级菜单查询
                .and(id -> {
                    if (reqVO.getCategoryIds()!=null && !reqVO.getCategoryIds().isEmpty()) {
                        for (String categoryId : reqVO.getCategoryIds()) {
                            id.or().eq("category.id", categoryId);
                        }
                    }else{
                        id.or().eq("category.id", 0);
                    }
                });


         //左连接分类表
           query.leftJoin(MarketingCategoryDO.class,"category",MarketingCategoryDO::getId, MarketingContentDO::getCategoryId)
                //.eq(reqVO.getCategoryId() != null, MarketingCategoryDO::getId, reqVO.getCategoryId())
                //左连接转发表
                .leftJoin(MarketingTransmitDO.class,"transmit",MarketingTransmitDO::getContentId, MarketingContentDO::getId)
                //左连接营销访客数据表
                //.leftJoin(MarketingVisitorDO.class,"visitor",MarketingVisitorDO::getContentId, MarketingContentDO::getId)

                 // 添加 Lambda QueryWrapper 条件
                 // 如果 reqVO 中包含 parmDept 参数，加入 FIND_IN_SET(parmDept, dept) > 0 条件
                .apply(ObjectUtil.isNotNull(reqVO.getDeptId()) && !reqVO.getDeptId().isEmpty(),
                        "FIND_IN_SET(" + reqVO.getDeptId() + ",category.dept_id) > 0")



                //.orderBy(StringUtils.hasText(reqVO.getOrderColumn()), reqVO.getOrderType(),"t."+reqVO.getOrderColumn())
               .orderBy(StringUtils.hasText(reqVO.getOrderColumn()), reqVO.getOrderType(),reqVO.getOrderColumn())




        .groupBy(MarketingContentDO::getId); // 避免因为连表查询而导致数据重合为一条

        return selectJoinPage(reqVO, MarketingContentRespByTypeVO.class, query);
    }



    default PageResult<MarketingContentRespByTypeVO> selectOne(Integer id) {

        MPJLambdaWrapper<MarketingContentDO> query = new MPJLambdaWrapper<MarketingContentDO>()
                .selectAll(MarketingContentDO.class)//主表字段
                .eq(id!=null, MarketingContentDO::getId, id);
        return selectJoinPage(new MarketingContentPageReqByTypeVO(), MarketingContentRespByTypeVO.class, query);
    }

    //@TenantIgnore
    default PageResult<MarketingContentDO> notCheckDataPermissionTenant(MarketingContentPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<MarketingContentDO>()
                .eqIfPresent(MarketingContentDO::getCategoryId, reqVO.getCategoryId())
                .eqIfPresent(MarketingContentDO::getMarketingType, reqVO.getMarketingType())
                .eqIfPresent(MarketingContentDO::getDeptId, reqVO.getDeptId())
                .likeIfPresent(MarketingContentDO::getDeptName, reqVO.getDeptName())
                .eqIfPresent(MarketingContentDO::getTitle, reqVO.getTitle())
                .likeIfPresent(MarketingContentDO::getName, reqVO.getName())
                .eqIfPresent(MarketingContentDO::getContent, reqVO.getContent())
                .eqIfPresent(MarketingContentDO::getSource, reqVO.getSource())
                .eqIfPresent(MarketingContentDO::getIsIssue, reqVO.getIsIssue())
                .eqIfPresent(MarketingContentDO::getCoverUrl, reqVO.getCoverUrl())
                .eqIfPresent(MarketingContentDO::getPicturesUrl, reqVO.getPicturesUrl())
                .eqIfPresent(MarketingContentDO::getVideoUrl, reqVO.getVideoUrl())
                .eqIfPresent(MarketingContentDO::getDisplayUrl, reqVO.getDisplayUrl())
                .eqIfPresent(MarketingContentDO::getMaterialType, reqVO.getMaterialType())
                .eqIfPresent(MarketingContentDO::getRemarkV, reqVO.getRemarkV())
                .betweenIfPresent(MarketingContentDO::getCreateTime, reqVO.getCreateTime())
                .orderByAsc(MarketingContentDO::getCreateTime));
    }

}